﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Models;
using MySql.Data.MySqlClient;
using Common;

namespace DAL
{
    /// <summary>
    /// 装饰线条数据处理
    /// </summary>
    public  class ZsLineDAL
    {
        //查询所有
        public DataTable ListAll(Dictionary<string,string> dic) {
            string sql =string.Empty;
            if (!string.IsNullOrWhiteSpace(dic["zsbh"])) {
                sql += " and zsbh like '%" + dic["zsbh"] + "%'";
            }
            if (!string.IsNullOrWhiteSpace(dic["zsmc"]))
            {
                sql += " and zsmc like '%" + dic["zsmc"] + "%'";
            }
            return SqlHelper.ExecuteDataTable("select * from zsxt where isdel= @isdel "+sql +" order by edittime desc", new MySqlParameter("@isdel", "0"));
        }
        //转换成Model
        public ZsLine ToZsLine(DataRow dr ) {
            ZsLine zl = new ZsLine();
            zl.zid = Convert.ToInt32(dr["zid"]);
            zl.zsbh = dr["zsbh"].ToString();
            zl.zsmc = dr["zsmc"].ToString();
            zl.zsdj = dr["zsdj"].ToString();
            zl.zslb = Convert.ToInt32(dr["zslb"]);
            zl.xtcd = dr["xtcd"].ToString();
            zl.oldPrice = dr["oldprice"].ToString();
            zl.Isdel = Convert.ToInt32(dr["isdel"]);
            return zl;
        }
        //保存数据
        public int SaveData(ZsLine zl) {
            int result = -1;
            result = SqlHelper.ExecuteNonQuery("insert into zsxt(zsbh,zsmc,zsdj,zslb,isdel,addTime,editTime,addUser,editUser,xtcd,oldprice) values(@zsbh,@zsmc,@zsdj,@zslb,@isdel,@addTime,@editTime,@addUser,@editUser,@xtcd,@oldprice)",
                new MySqlParameter("@zsbh",zl.zsbh),new MySqlParameter("@zsmc",zl.zsmc),new MySqlParameter("@zsdj",zl.zsdj),new MySqlParameter("@zslb",zl.zslb),new MySqlParameter("@isdel","0"),
                new MySqlParameter("@addTime", DateTime.Now.ToString()), new MySqlParameter("@editTime", DateTime.Now.ToString()), new MySqlParameter("@addUser", RemeberName.Username), new MySqlParameter("@editUser", RemeberName.Username),
                 new MySqlParameter("@xtcd", zl.xtcd), new MySqlParameter("@oldprice", zl.oldPrice));
            return result;
        }
        //更新数据
        public int UpdateData(ZsLine zl) {
            int result = -1;
            result = SqlHelper.ExecuteNonQuery("update zsxt set zsbh=@zsbh,zsmc=@zsmc,zsdj=@zsdj,zslb=@zslb,editTime=@editTime,editUser=@editUser,xtcd=@xtcd,oldprice=@oldprice where zid =@zid",
                new MySqlParameter("@zsbh", zl.zsbh), new MySqlParameter("@zsmc", zl.zsmc), new MySqlParameter("@zsdj", zl.zsdj), new MySqlParameter("@zslb", zl.zslb),
                new MySqlParameter("@editTime", DateTime.Now.ToString()), new MySqlParameter("@editUser", RemeberName.Username), new MySqlParameter("@xtcd", zl.xtcd),
                new MySqlParameter("@oldprice", zl.oldPrice), new MySqlParameter("@zid", zl.zid));
            return result;
        }
        //删除数据
        public int DelData(string zid) {
            int result = -1;
            result = SqlHelper.ExecuteNonQuery("update zsxt set isdel = 1,editTime=@editTime,editUser=@editUser where zid =@zid",
                new MySqlParameter("@editTime", DateTime.Now.ToString()), new MySqlParameter("@editUser", RemeberName.Username), new MySqlParameter("zid", zid));
            return result;
        }
        //根据id查找数据
        public ZsLine GetById(string zid) {
            return ToZsLine(SqlHelper.ExecuteDataTable("select * from zsxt where zid = @zid",new MySqlParameter("@zid",zid)).Rows[0]);
        }
    }
}
